var active;

layui.config({
    base: '../../../../assets/libs/', version: true
}).extend({
    index: 'lib/index',//主入口模块
    activeForm: '../lib/activeForm/activeForm'
}).use(['jquery', 'index', 'table', 'form', 'laydate', 'layer', 'activeForm'], function () {

    var layuipackage = layui.layuipackage,
        laiease = layui.laiease,
        table = layui.table,
        form = layui.form,
        admin = layui.admin,
        $ = layui.jquery,
        layer = layui.layer,
        activeForm = layui.activeForm,
        laydate = layui.laydate;

    var subId = laiease.utils.geturlparm("id");

    var substationName = decodeURIComponent(laiease.utils.geturlparm("substationName"));

    $('#substationName').val(substationName);

    var __stDetail = active = {
        _static: {
            substation_connect_type: []
        },
        _init_: function () {
            this._data._init_();
            this._bind._init_();

            setTimeout(function () {
                $('.hidden').css('opacity', 1);
            }, 150);

        },
        _data: {
            _init_: function () {
                var that = this;

                $.when(
                    __stDetail._server.getVoltageLevel(),
                    __stDetail._server.getMainLine(),
                    admin.req({
                        type: 'get',
                        data: {
                            groupKey: 'substation_connect_type'
                        },
                        url: laiease.webServer + '/web/mdic/selectListByGroupKey',
                    }),
                    admin.req({
                        type: 'get',
                        data: {},
                        url: laiease.webServer + '/web/substation/getOneSubstationConnect/' + subId,
                    }))
                    .then(function (dianya, xianlu, lianjiefangshi_zidian, detail) {

                        __stDetail._static.data_voltage_level = dianya[0].data;

                        __stDetail._static.data_main_line = xianlu[0].data;

                        __stDetail._static.substation_connect_type = lianjiefangshi_zidian[0].data;

                        __stDetail._static.huixianArr = detail[0].data;

                        __stDetail._static.activeFormInst = new activeForm({
                            el: '.editor-tabel-container',
                            disabled: false,
                            columns: [
                                {
                                    field: 'checkbox',
                                    type: 'checkbox',
                                },
                                {
                                    title: '电压等级',
                                    type: 'select',
                                    field: 'voltage',
                                    required: true,
                                    data: __stDetail._static.data_voltage_level.map(function (item) {
                                        return {
                                            value: item.catalog,
                                            name: item.dicName,
                                        }
                                    }),
                                    onChange: function (e) {

                                        __stDetail._static.activeFormInst.setCellOfSameRow(e, {
                                            field: 'line',
                                            data: __stDetail._static.data_main_line.filter(function (lineItem) {
                                                return e.value === lineItem.voltageLevelCatalog;
                                            }).map(function (item) {
                                                return {
                                                    value: item.lineId,
                                                    name: item.lineName,
                                                }
                                            })
                                        })

                                    }
                                },
                                {
                                    title: '线路名称',
                                    type: 'select',
                                    field: 'line',
                                    data: [],
                                    required: true,
                                    onChange: function (e) {
                                        __stDetail._server.getTowersByLineId({lineId: e.value}).then(function (res_data) {

                                            __stDetail._static.activeFormInst.setCellOfSameRow(e, {
                                                field: 'tower',
                                                data: res_data.map(function (item) {
                                                    return {
                                                        name: '#' + item.towerNo,
                                                        value: item.towerId,
                                                    }
                                                })
                                            })

                                        })
                                    }
                                },
                                {
                                    title: '杆塔号',
                                    type: 'select',
                                    field: 'tower',
                                    required: true,
                                    data: [],

                                },
                                {
                                    title: '链接方式',
                                    type: 'select',
                                    field: 'connect_type',
                                    required: true,
                                    data: __stDetail._static.substation_connect_type.map(function (item) {
                                        return {
                                            value: item.catalog,
                                            name: item.dicName,
                                        }
                                    }),
                                },
                            ],
                        });


                        if (__stDetail._static.huixianArr.length) {

                            $.when.apply(null, __stDetail._static.huixianArr.map(function (prevDataItem) {
                                return __stDetail._server.getTowersByLineId({
                                    lineId: prevDataItem.lineId
                                });
                            })).then(function () {
                                var args = Array.prototype.slice.call(arguments);

                                __stDetail._static.huixianArr = __stDetail._static.huixianArr.map(function (huixianItem, huixianItemIndex) {

                                    huixianItem.form_00 = __stDetail._static.data_voltage_level;

                                    huixianItem.form_01 = __stDetail._static.data_main_line.filter(function (item) {
                                        return item.voltageLevelCatalog == huixianItem.voltageLevelCatalog;
                                    });

                                    huixianItem.form_02 = args[huixianItemIndex];

                                    huixianItem.form_03 = __stDetail._static.substation_connect_type;

                                    return huixianItem;

                                });

                                __stDetail._static.activeFormInst.setValue(__stDetail._static.huixianArr.map(function (dataItem) {

                                    return {
                                        checkbox:{
                                            // disabled: true,
                                        },
                                        voltage: {
                                            // disabled: true,
                                            data: dataItem.form_00.map(function (item) {
                                                return {
                                                    value: item.catalog,
                                                    name: item.dicName,
                                                    selected: item.catalog == dataItem.voltageLevelCatalog
                                                }
                                            })
                                        },
                                        line: {
                                            // disabled: true,
                                            data: dataItem.form_01.map(function (item) {
                                                return {
                                                    value: item.lineId,
                                                    name: item.lineName,
                                                    selected: item.lineId == dataItem.lineId
                                                }
                                            })
                                        },
                                        tower: {
                                            // disabled: true,
                                            data: dataItem.form_02.map(function (item) {
                                                return {
                                                    value: item.towerId,
                                                    name: '#' + item.towerNo,
                                                    selected: item.towerId == dataItem.towerId
                                                }
                                            })
                                        },
                                        connect_type: {
                                            // disabled: true,
                                            data: dataItem.form_03.map(function (item) {
                                                return {
                                                    value: item.catalog,
                                                    name: item.dicName,
                                                    selected: item.dicName == dataItem.connectType
                                                }
                                            })
                                        },
                                    }
                                }));

                            });

                        }

                    });

            },

        },

        _bind: {
            _init_: function () {

            },

        },
        _tool: {

            getJSONData: function () {

                var arr = __stDetail._static.activeFormInst.getValue();

                var dataToSubmit = arr.map(function (item) {
                    return {
                        voltageLevelCatalog: item['voltage'].value,
                        lineId: item['line'].value,
                        towerId: item['tower'].value,
                        connectType: item['connect_type'].name
                    }
                });

                dataToSubmit.forEach(function (item) {
                    for (var itemKey in item) {
                        if (item.hasOwnProperty(itemKey)) {
                            if (!item[itemKey]) {
                                throw new Error('请填写完整')
                            }
                        }
                    }
                })

                return {
                    id: subId,
                    connectList: dataToSubmit
                }

            },
        },
        _server: {

            /**
             * 获取电压
             * @param data
             * @param success
             */
            getVoltageLevel: function (data, success) {
                return admin.req({
                    type: 'get',
                    data: {
                        groupKey: 'voltageLevel'
                    },
                    url: laiease.webServer + '/web/mdic/selectListByGroupKey',
                });
            },

            /**
             * 获取详情
             * @param data
             * @param success
             */
            getOneSubstationConnect: function (data, success) {
                admin.req({
                    type: 'get',
                    // data: data,
                    url: laiease.webServer + '/web/substation/getOneSubstationConnect/' + data,
                    done: function (res) {
                        success(res);
                    }
                });
            },

            /**
             * 获取杆塔位置
             * @param data
             * @param success
             */
            getConnectTypeList: function (data, success) {
                admin.req({
                    type: 'get',
                    data: {
                        groupKey: 'substation_connect_type'
                    },
                    url: laiease.webServer + '/web/mdic/selectListByGroupKey',
                    done: function (res) {
                        success(res);
                    }
                });
            },

            /**
             * 获取全部线路
             * @param data
             * @param success
             */
            getMainLine: function (data, success) {
                return admin.req({
                    type: 'get',
                    data: null,
                    url: laiease.webServer + '/web/line/selectMainLine',
                });
            },

            /**
             * 根据线路id lineId查询杆塔
             * @param data
             * @param success
             */
            getTowersByLineId: function (data, success) {
                var dtd = $.Deferred();

                admin.req({
                    type: 'get',
                    data: data,
                    url: laiease.webServer + '/web/tower/selectTowerByLineId',
                    done: function (res) {
                        // success(res);
                        dtd.resolve(res);
                    }
                });

                return dtd.promise();
            },
        }
    };
    __stDetail._init_();
});